﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class LuceneSearchInstruction:InstructionBase
    {
        #region props
        private string _IndexingFilePath;
        /// <summary></summary>
        public string IndexingFilePath
        {
            get
            {
                return this._IndexingFilePath;
            }
            set
            {
                this._IndexingFilePath = value;
            }
        }

        private string _KeyField;
        /// <summary></summary>
        public string KeyField
        {
            get
            {
                return this._KeyField;
            }
            set
            {
                this._KeyField = value;
            }
        }

        private string _TitleField;
        /// <summary></summary>
        public string TitleField
        {
            get
            {
                return this._TitleField;
            }
            set
            {
                this._TitleField = value;
            }
        }

        private string _Query;
        /// <summary></summary>
        public string Query
        {
            get
            {
                return this._Query;
            }
            set
            {
                this._Query = value;
            }
        }

        private Dictionary<string,double> _FieldWeights;
        /// <summary></summary>
        public Dictionary<string,double> FieldWeights
        {
            get
            {
                return this._FieldWeights;
            }
            set
            {
                this._FieldWeights = value;
            }
        }

        private string _SearchResultFilePath;
        /// <summary></summary>
        public string SearchResultFilePath
        {
            get
            {
                return this._SearchResultFilePath;
            }
            set
            {
                this._SearchResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public LuceneSearchInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._IndexingFilePath = string.Empty;
            this._KeyField = string.Empty;
            this._TitleField = string.Empty;
            this._Query = string.Empty;
            this._FieldWeights=new Dictionary<string, double>();
            this._SearchResultFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="indexFilePath"></param>
        /// <param name="keyField"></param>
        /// <param name="titleField"></param>
        /// <param name="query"></param>
        /// <param name="searchResultFilePath"></param>
        /// <param name="fieldWeights"></param>
        public LuceneSearchInstruction(Guid componentID, Guid jobTicketID,
            string indexFilePath, string keyField, string titleField, string query,
            string searchResultFilePath,
            Dictionary<string,double> fieldWeights) 
            : base(componentID, jobTicketID)
        {
            this._IndexingFilePath = indexFilePath;
            this._KeyField = keyField;
            this._TitleField = titleField;
            this._Query = query;
            this._SearchResultFilePath = searchResultFilePath;
            this._FieldWeights = fieldWeights;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IndexFilePath", this.IndexingFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField", this.KeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TitleField",this.TitleField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Query", this.Query);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"SearchResultFilePath",this.SearchResultFilePath);
            if (this.FieldWeights != null && this.FieldWeights.Count > 0)
            {
                XmlNode fieldNodes = XmlDataUtil.AddElement(ref xDoc, xNode, "Fields");
                foreach (string fieldName in this.FieldWeights.Keys)
                {
                    XmlNode fieldNode = XmlDataUtil.AddElement(ref xDoc, fieldNodes, "Field");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "FieldName", fieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, fieldNode, "Weight", this.FieldWeights[fieldName].ToString());
                }
            }

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                LuceneSearchInstruction instruction = new LuceneSearchInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.IndexingFilePath = XmlDataUtil.GetAttributeValue(xNode, "IndexFilePath", "");
                instruction.KeyField = XmlDataUtil.GetAttributeValue(xNode, "KeyField", "");
                instruction.TitleField = XmlDataUtil.GetAttributeValue(xNode, "TitleField", "");
                instruction.Query = XmlDataUtil.GetAttributeValue(xNode, "Query", "");
                instruction.SearchResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "SearchResultFilePath", "");
                XmlNodeList fieldNodes = xNode.SelectNodes("Fields/Field");
                if (fieldNodes != null && fieldNodes.Count > 0)
                {
                    instruction.FieldWeights=new Dictionary<string, double>();
                    foreach (XmlNode fieldNode in fieldNodes)
                    {
                        string fieldName = XmlDataUtil.GetAttributeValue(fieldNode, "FieldName", "");
                        double weight = double.Parse(XmlDataUtil.GetAttributeValue(fieldNode, "Weight", "0"));
                        instruction.FieldWeights.Add(fieldName,weight);
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
